﻿@{
    CloudStorageLight.Core.BlobUser user = null;
    if (System.Web.HttpContext.Current.User.Identity.IsAuthenticated) {
        user = new CloudStorageLight.SharePointWeb.Models.AppBlobUser(System.Web.HttpContext.Current.User);
    }
    var clientDownloadPage = Url.Content("https://www.cloudstoragelight.com/explorer/publish.htm");
    var cloudAccount = CloudStorageLight.Core.BlobService.GetCurrentCloudAccount();
    if (cloudAccount != null && !string.IsNullOrEmpty(cloudAccount.ClientDownloadPage))
    {
        clientDownloadPage = cloudAccount.ClientDownloadPage;
    }
    var webparts = this.Context.Items.Contains("WebParts");
    var username = user == null ? "" : "(" + user.Name.Split('@')[0] + ")";

}
<div class="ms-core-header ms-navedit-titleBoxContainer" id="colorSample" style="display:none;" />
<script type="text/javascript">
    @Html.Partial("_Message")


    var hostweburl;
    $(function () {


        //Get the URI decoded URL.
        hostweburl =
            decodeURIComponent(
                getQueryStringParameter("SPHostUrl")
        );

        // The SharePoint js files URL are in the form:
        // web_url/_layouts/15/resource
        var scriptbase = hostweburl + "/_layouts/15/";

        if (getQueryStringParameter("SPHostUrl")) {
            // Load the js file and continue to the
            //   success handler
            $.getScript(scriptbase + "SP.UI.Controls.js", renderChrome);
        }

    });

    // Callback for the onCssLoaded event defined
    //  in the options object of the chrome control
    function chromeLoaded() {

        var color = $("#colorSample");
        var bg = color.css("background-color");
        var fc = color.css("color");
        $("body").css("background-color", bg);
        $("body").css("color", fc);
        $("#file-items").css("background-color", bg);
        $("<style>a.blobfile { color: " + fc + "; }</style>").appendTo("head");
        $(".navigation-bar").css("background-color", bg);
        $(".navigation-bar a").css("color", fc);
        $(".dropdown-menu a").css("color", "black");

        // When the page has loaded the required
        //  resources for the chrome control,
        //  display the page body.
        $("#appTitle").show();
        $("#chromeControl_topheader_apptitlelink").unbind().click(function () {
            window.location.href = '@(Url.RouteUrl("files", new { id = "" }) + "/?")' + document.URL.split("?")[1];
            return false;
        });
    }

    // Render Option
    function renderChromeOption() {
    }

    //Function to prepare the options and render the control
    function renderChrome() {
        // The Help, Account and Contact pages receive the
        //   same query string parameters as the main page
        var options = {
            "appTitle": "Cloud Storage Light " + "@(username)",
            //"appHelpPageUrl": "Help.html?"
            //    + document.URL.split("?")[1],
            // The onCssLoaded event allows you to
            //  specify a callback to execute when the
            //  chrome resources have been loaded.
            "onCssLoaded": "chromeLoaded()",
            "settingsLinks": [
                @if(user != null && !user.IsGuest)
                {
                    <text>
                    {
                        "linkUrl": "@(Url.RouteUrl("files", new { id = "" }) + "/")?" + document.URL.split("?")[1],
                        "displayName": "Home"
                    },
                    </text>
                }
                @if (user != null && !user.IsGuest && !this.Request.Browser.IsMobileDevice)
                {
                    <text>
                    {
                        "linkUrl": "@(clientDownloadPage)",
                        "displayName": "@(CloudStorageLight.Core.Web.ViewMessages.MsgExploreTool)"
                    },
                    </text>
                }
                @if (user != null && user.IsAdmin)
                {
                    <text>
                    {
                        "linkUrl": "@(Url.RouteUrl("Settings", new { Action = "LogView" }))?" + document.URL.split("?")[1],
                        "displayName": "@(CloudStorageLight.Core.Web.ViewMessages.MsgLogView)"
                    },
                    {
                        "linkUrl": "@(Url.RouteUrl("Settings", new { Action = "CloudSettings" }))?" + document.URL.split("?")[1],
                        "displayName": "@(CloudStorageLight.Core.Web.ViewMessages.CmdCloudAccountSettings)"
                    },
                    {
                        "linkUrl": "@(Url.RouteUrl("Settings", new { Action = "Configuration" }))?" + document.URL.split("?")[1],
                        "displayName": "@(CloudStorageLight.Core.Web.ViewMessages.MsgStorageConfiguration)"
                    }
                    </text>
                }
            ]
        };

        @if (!webparts)
        {
            <text>
        var nav = new SP.UI.Controls.Navigation(
                                "chrome_ctrl_placeholder",
                                options
                            );
        nav.setVisible(true);
        //$("#chromeControl_bottomheader_apptitle").html($("#appTitle"));
        $("#chromeControl_bottomheader").remove();

        renderChromeOption();
        </text>
        }

    }

    // Function to retrieve a query string value.
    // For production purposes you may want to use
    //  a library to handle the query string.
    function getQueryStringParameter(paramToRetrieve) {
        if (document.URL.split("?").length <= 1) return undefined;

        var params =
            document.URL.split("?")[1].split("&");
        var strParams = "";
        for (var i = 0; i < params.length; i = i + 1) {
            var singleParam = params[i].split("=");
            if (singleParam[0] == paramToRetrieve)
                return singleParam[1];
        }
    }

</script>

